package linwen.wang.tdddemo;

import linwen.wang.tdddemo.common.UserInfoIsNullException;
import linwen.wang.tdddemo.entity.UserInfoEntity;
import linwen.wang.tdddemo.repository.UserRepository;
import linwen.wang.tdddemo.service.UserInfoService;
import linwen.wang.tdddemo.vo.UserInfo;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import static org.mockito.Mockito.verify;

/**
 * @ClassName MockApplication
 * @Description TODO
 * @Author zero
 * @DATE 2023/5/4 10:45 PM
 * @Version 1.0
 */

@RunWith(MockitoJUnitRunner.class)
public class MockApplication {

    @InjectMocks
    UserInfoService userInfoService;
    @Mock
    UserRepository userRepository;

    @Test
    public void getUserInfo(){
        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setName("xiaoming");
        userInfoEntity.setId(123L);
        userInfoEntity.setPhoneNumber("15322222222");
        UserInfoEntity userInfoEntity2 = new UserInfoEntity();
        userInfoEntity2.setName("zhangsan");
        userInfoEntity2.setId(123L);
        userInfoEntity2.setPhoneNumber("15322222222");
        Mockito.when(userRepository.getUserInfo(Mockito.anyLong())).thenReturn(userInfoEntity).thenReturn(userInfoEntity2);
        UserInfo result = userInfoService.getUserInfo(123L);
        System.out.println(result);
        Assert.assertEquals("id 不一致", userInfoEntity.getId(), result.getId());
        Assert.assertEquals("name 不一致", userInfoEntity.getName(), result.getName());
        Assert.assertEquals("PhoneNumber 不一致", userInfoEntity.getPhoneNumber(), result.getPhoneNumber());
        UserInfo result2 = userInfoService.getUserInfo(456L);
        System.out.println(result2);
    }

    @Test
    public void saveUserInfo(){
        UserInfo userInfo = new UserInfo();
        userInfo.setName("xiaoming");
        userInfo.setPhoneNumber("15322222222");
        userInfoService.saveUserInfo(userInfo);
        ArgumentCaptor<UserInfoEntity> userInfoEntityArgumentCaptor = ArgumentCaptor.forClass(UserInfoEntity.class);
        verify(userRepository).save(userInfoEntityArgumentCaptor.capture());
        Assert.assertEquals("name 不一致", userInfo.getName(), userInfoEntityArgumentCaptor.getValue().getName());
        Assert.assertEquals("PhoneNumber 不一致", userInfo.getPhoneNumber(), userInfoEntityArgumentCaptor.getValue().getPhoneNumber());
    }

    @Test(expected = UserInfoIsNullException.class)
    public void getUserInfo1() throws Exception {
        userInfoService.getUserInfo(111l);
    }


}
